let Subscribe = function () {
    this.init = function () {
        $.init()
        this.fn.subscribe_state();
        this.events();
        return this;
    }
    this.events = function () {
        /**
         * 订阅
         */
        $(document).on('click', '.submit', function () {
            ajax({
                url: '/api/taijiao/subscribe',
                type: 'post',
                data: {
                    openid: gdata.openid,
                    pushTime: $('[name=pushTime]').val().replace(' : ', ''),
                    last_menstrual_day: $('[name=last_menstrual_day]').val().replace(/-/g, '')
                },
                success(res) {
                    if (res === 'ok') {
                        alert('订阅成功')
                        location.reload()
                    }
                }
            })
        }).on('change', 'input', function () {
            if ($('[name=pushTime]').val() && $('[name=last_menstrual_day]').val()) {
                $('.btn.submit').removeAttr('disabled')
            } else {
                $('.btn.submit').attr('disabled', 'disabled')
            }
        })
    }
    this.fn = {
        /**
         * 取消订阅
         */
        unsubscribe() {
            ajax({
                url: '/api/taijiao/unsubscribe',
                type: 'POST',
                data: { openid: gdata.openid }
            })
        },
        subscribe_state: function () {
            ajax({
                url: `/api/subscribe_state`,
                type: 'post',
                data: { openid: gdata.openid },
                success(res) {
                    $('form').show()
                    if (res.taijiaoState == 1) {
                        //已订阅
                        $('[name=pushTime]').attr('disabled', 'disabled').val(res.taijiaoPushTime.match(/\d{2}/g).join(':'))
                        $('[name=last_menstrual_day]').attr('disabled', 'disabled').val(new Date(res.pregnancy.startDate.match(/\d{4}-\d{2}-\d{2}/)[0]).format('yyyy-MM-dd'))
                        $('.btn.submit').text('已订阅').attr('disabled', 'disabled').show()
                        let msg = `今天：${new Date().format('yyyy-MM-dd')}\n
                                <p>您已怀孕：${res.pregnancy.days || '--'}天（${res.pregnancy.weekTips || '--'}）（${res.pregnancy.monthTips || '--'}）</p>
                                <p>预产期：${new Date(res.pregnancy.childbirthDate.match(/\d{4}-\d{2}-\d{2}/)[0]).format('yyyy-MM-dd') || '--'}</p>
                                <p>距离预产期：${res.pregnancy.remainDays || '--'}天</p>
                                <p>已为您推送胎教服务：${res.taijiaoPushCount || '0'}次</p>`
                        $('.row.header').html(msg)
                    } else {
                        //未订阅
                        $('.row.header').html('未订阅')
                        $('.btn.submit').text('订阅胎教').show()
                        let timeArr = ['19:00', '19:30', '20:00', '20:30', '21:00', '21:30', '22:00']
                        let time = timeArr[parseInt(Math.random() * timeArr.length)]
                        $('[name=pushTime]').val(time.replace(':', ' : '))
                        $("#picker").picker({
                            toolbarTemplate: '<header class="bar bar-nav">\
                                                <button class="button button-link pull-right close-picker">确定</button>\
                                                <h1 class="title">请选择推送时间</h1>\
                                            </header>',
                            cols: [
                                {
                                    textAlign: 'center',
                                    values: ['07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23']
                                },
                                {
                                    textAlign: 'center',
                                    values: [':']
                                },
                                {
                                    textAlign: 'center',
                                    values: ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59']
                                }
                            ]
                        });
                        $("#datetime-picker").calendar({
                            minDate: '2017-01-01',
                            maxDate: '2020-01-21'
                        });
                    }
                }
            })
        },

    }
}
var subscribe = new Subscribe().init();
